"""
Case Type   : 兼容M*数据库tiem函数
Case Name   : time()函数入参为datetime类型，不同输入格式测试
Create At   : 2023.01.06
Owner       : z@wan005
Description :
    1、参数为datetime类型，格式为'YYYY-MM-DD hh:mm:ss[.frac]
    2、参数为datetime类型，格式为'YY-MM-DD hh:mm:ss[.frac]'
    3、参数为datetime类型,格式为'YYYYMMDDhhmmss'
    4、参数为datetime类型,格式为'YYMMDDhhmmss'
    5、参数为datetime类型,格式为YYYYMMDDhhmmss
    6、参数为datetime类型，格式为YYMMDDhhmmss
    7、清理环境
Expect      :
    1、成功
    2、成功
    3、成功
    4、成功
    5、成功
    6、成功
    7、成功
History     :modified by zhanghuan96，2024.06.28，优化断言
"""

import os
import unittest

from testcase.utils.Common import Common
from testcase.utils.CommonSH import CommonSH
from testcase.utils.Constant import Constant
from testcase.utils.Logger import Logger


class CompatibilityTest02(unittest.TestCase):

    def setUp(self):
        self.log = Logger()
        self.common = Common()
        self.sh_primary = CommonSH('PrimaryDbUser')
        self.constant = Constant()
        self.db_name = "db_time_0002"

    def test_time(self):
        self.log.info(f'-----{os.path.basename(__file__)} start-----')
        self.log.info('----前置条件：创建兼容b库---')
        sql_cmd = f"drop database if exists {self.db_name};" \
                  f"create database {self.db_name} dbcompatibility 'b';"
        self.log.info(sql_cmd)
        sql_res = self.sh_primary.execut_db_sql(sql_cmd)
        self.log.info(sql_res)
        self.assertIn(self.constant.CREATE_DATABASE_SUCCESS, sql_res)

        text = "--step1:参数为datetime类型，格式为'YYYY-MM-DD hh:mm:ss[.frac];" \
               "expect:成功--"
        self.log.info(text)
        sql_cmd = "select time('0000-01-01 23:59:59');" \
                  "select time('4714-11-24 00:00:00');"
        self.log.info(sql_cmd)
        sql_res = self.sh_primary.execut_db_sql(sql_cmd,
                                                dbname=f'{self.db_name}')
        self.log.info(sql_res)
        self.assertTrue(len(sql_res.strip().splitlines()) >= 9, '行数不到9行')
        self.assertEqual('23:59:59', sql_res.splitlines()[2].strip(),
                         '执行失败' + text)
        self.assertEqual('00:00:00', sql_res.splitlines()[-2].strip(),
                         '执行失败' + text)

        text = "--step2:参数为datetime类型，格式为'YY-MM-DD hh:mm:ss[.frac]';" \
               "expect:成功--"
        self.log.info(text)
        sql_cmd = "select time('69-01-01 23:59:59');" \
                  "select time('99-01-01 23:59:59');"
        self.log.info(sql_cmd)
        sql_res = self.sh_primary.execut_db_sql(sql_cmd,
                                                dbname=f'{self.db_name}')
        self.log.info(sql_res)
        self.assertTrue(len(sql_res.strip().splitlines()) >= 8, '行数不到9行')
        self.assertEqual(2, sql_res.count('23:59:59'), '执行失败' + text)

        text = "--step3:参数为datetime类型,格式为'YYYYMMDDhhmmss';expect:成功--"
        self.log.info(text)
        sql_cmd = "select time('99991231235959.999999');"
        self.log.info(sql_cmd)
        sql_res = self.sh_primary.execut_db_sql(sql_cmd,
                                                dbname=f'{self.db_name}')
        self.log.info(sql_res)
        self.assertTrue(len(sql_res.strip().splitlines()) >= 4, '行数不到4行')
        self.assertEqual('23:59:59.999999', sql_res.splitlines()[-2].strip(),
                         '执行失败' + text)

        text = "--step4:参数为datetime类型,格式为'YYMMDDhhmmss';expect:成功--"
        self.log.info(text)
        sql_cmd = "select time('991231235959');"
        self.log.info(sql_cmd)
        sql_res = self.sh_primary.execut_db_sql(sql_cmd,
                                                dbname=f'{self.db_name}')
        self.log.info(sql_res)
        self.assertTrue(len(sql_res.strip().splitlines()) >= 4, '行数不到4行')
        self.assertEqual('23:59:59', sql_res.splitlines()[-2].strip(),
                         '执行失败' + text)

        text = "--step5:参数为datetime类型,格式为YYYYMMDDhhmmss;expect:成功--"
        self.log.info(text)
        sql_cmd = "select time(20691231235959);"
        self.log.info(sql_cmd)
        sql_res = self.sh_primary.execut_db_sql(sql_cmd,
                                                dbname=f'{self.db_name}')
        self.log.info(sql_res)
        self.assertTrue(len(sql_res.strip().splitlines()) >= 4, '行数不到4行')
        self.assertEqual('23:59:59', sql_res.splitlines()[-2].strip(),
                         '执行失败' + text)

        text = "--step6:参数为datetime类型，格式为YYMMDDhhmmss;expect:成功--"
        self.log.info(text)
        sql_cmd = "select time(701231235959);"
        self.log.info(sql_cmd)
        sql_res = self.sh_primary.execut_db_sql(sql_cmd,
                                                dbname=f'{self.db_name}')
        self.log.info(sql_res)
        self.assertTrue(len(sql_res.strip().splitlines()) >= 4, '行数不到4行')
        self.assertEqual('23:59:59', sql_res.splitlines()[-2].strip(),
                         '执行失败' + text)

        text = "--step6:dateime显式转换;expect:成功--"
        self.log.info(text)
        sql_cmd = "select time(datetime'701231235959.9');" \
                  "select time(701231235959.9::datetime);" \
                  "select time(cast(701231235959.9 as datetime));"
        self.log.info(sql_cmd)
        sql_res = self.sh_primary.execut_db_sql(sql_cmd,
                                                dbname=f'{self.db_name}')
        self.log.info(sql_res)
        self.assertTrue(len(sql_res.strip().splitlines()) >= 14, '行数不到14行')
        self.assertEqual(1, sql_res.count('23:59:59.9'), '执行失败' + text)
        self.assertEqual(2, sql_res.count('00:00:00'), '执行失败' + text)

    def tearDown(self):
        text = '----step7:清理环境;expect:成功----'
        self.log.info(text)
        sql_cmd = f"drop database {self.db_name};"
        self.log.info(sql_cmd)
        drop_res1 = self.sh_primary.execut_db_sql(sql_cmd,
                                                  dbname='postgres')
        self.log.info(drop_res1)
        self.assertIn(self.constant.DROP_DATABASE_SUCCESS, drop_res1,
                      '执行失败' + text)
        self.log.info(f'-----{os.path.basename(__file__)} end-----')
